package apiwork.lcbmi.util.config;


import java.util.ArrayList;
import java.util.HashMap;

import org.dom4j.Element;

import com.linkage.util.config.MemoryConfig;
import com.linkage.util.config.SystemConfig;
import com.linkage.util.xml.XMLProperties;

public class DefaultProtocalAdaptorConfig extends MemoryConfig{
	private static DefaultProtocalAdaptorConfig m_instance = null;
	private static XMLProperties xmlProperties = null;
	
	private DefaultProtocalAdaptorConfig(){}
	
	public static DefaultProtocalAdaptorConfig getInstance() {
		if (m_instance == null) {
			m_instance = new DefaultProtocalAdaptorConfig();
		}
		return m_instance;
	}
	
	synchronized public void reload() throws Exception {
		try{
			if(isReload() || xmlProperties == null){
				String xmlFile = SystemConfig.getInstance().getConfig("DefaultProtocalAdaptor");
				xmlProperties = new XMLProperties(xmlFile);
			}
			setReload(false);
		}catch(Exception e){
			throw new Exception(e);
		}
	}
	
	public ArrayList<HashMap<String, Object>> getDatagramList() {
		ArrayList<HashMap<String, Object>> commandList = new ArrayList<HashMap<String, Object>>();
		ArrayList<Element> commandElementList;
		try {
			reload();
			commandElementList = (ArrayList<Element>) xmlProperties
//					.getElements("dictory.datagram");
			.getElements("dictionary.datagram");
			for (Element element : commandElementList) {
				commandList.add(xmlProperties.getAllSubElementInfo(element,true));
			}
		} catch (Exception e) {
		}
//		System.out.println("commandList:"+commandList);
		return commandList;
	}
	
	
	public String getProperties(String name) throws Exception{
		try{
			reload();
			return xmlProperties.getElementValue(name);
		}catch(Exception e){
			e.printStackTrace();
			return "";
		}
    }
}
